package com.mdc.admin.config;

import com.mdc.log.config.MDCThreadPoolTaskExecutor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置，并支持mdc
 *
 * @author pcheng reviewcode@163.com
 * @date 16:42 2021/8/1
 */
@Configuration
@EnableAsync
public class ExecutorConfiguration implements AsyncConfigurer {
    private static final String SITE_EXEC_NAME = "auth-portal-exec-";

    @Bean
    public ThreadPoolTaskExecutor customServiceExecutor() {
        MDCThreadPoolTaskExecutor executor = new MDCThreadPoolTaskExecutor();
        // 配置核心线程数
        executor.setCorePoolSize(10);
        // 配置最大线程数
        executor.setMaxPoolSize(20);
        // 配置队列大小
        executor.setQueueCapacity(1024);
        // 配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix(SITE_EXEC_NAME);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 执行初始化
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}
